package cn.myjerry.test.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;

/**
 * 测试时间处理(java.sql.Date, Time, Timestamp)
 * @author Jerry
 *
 */
public class Demo06 {
	// JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/testjdbc?useSSL=true&useUnicode=true&characterEncoding=utf8";
    
    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "Jerry1912317";
	
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		// 加载驱动类
		try {
			Class.forName(JDBC_DRIVER);
			conn = DriverManager.getConnection(DB_URL, USER, PASS);
			
			for(int i=0;i<1000;i++) {
				
				ps = conn.prepareStatement("INSERT INTO User (Name,Mobile,PassWord,CreateTime,LastLoginTime) VALUES (?,?,?,?,?)");
				ps.setObject(1, "Jerry" + i);
				ps.setObject(2, "13572042154");
				ps.setObject(3, "123456");
				
				int rand = 100 * i * (new Random().nextInt(100000000));
				
				java.sql.Date date = new java.sql.Date(System.currentTimeMillis()-rand);
				java.sql.Timestamp stamp = new java.sql.Timestamp(System.currentTimeMillis()-rand);
				ps.setTimestamp(4, stamp);
				ps.setDate(5, date);
				ps.execute();
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
